package com.goldplusgold.tradedelay.app.my.networkservice;

import android.content.ContentValues;
import android.database.Cursor;
import android.text.TextUtils;

import com.goldplusgold.database.lib.ApiTableClazzDeclare;
import com.goldplusgold.database.lib.DatabaseCache;
import com.goldplusgold.database.lib.SQLiteOpenManager;
import com.goldplusgold.network.lib.ApiProxyFactory;
import com.goldplusgold.network.lib.model.ErrorMessage;
import com.goldplusgold.tradedelay.app.my.db.DataBaseTable;
import com.goldplusgold.tradedelay.app.my.pojo.CodeInfo;
import com.goldplusgold.tradedelay.app.my.account.UserInfo;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/5/11.
 */
//todo 检查内部逻辑
public class MyService implements ApiTableClazzDeclare {
    private static MyService sSingltone;
    private final ApiMy mApiMy;
    private String mToken;
    private UserInfo mUserInfo;

    public MyService() {
        mApiMy = ApiProxyFactory.getProxy(ApiMy.class);
        DatabaseCache.getInstance().declareColumnsClass(this);
    }

    public static synchronized MyService getInstance() {
        if (sSingltone == null) {
            sSingltone = new MyService();
        }
        return sSingltone;
    }


    public String getToken() {
        return mToken;
    }

    public void setToken(String token) {
        this.mToken = token;
    }

    public UserInfo getmUserInfo() {
        return this.mUserInfo;
    }

    public void setUserInfo(UserInfo userInfo) {
        if (userInfo != null) {
            this.mUserInfo = userInfo;
        }
    }

    public boolean isLogin() {
        return !TextUtils.isEmpty(mToken);
    }

    public void cleanToken() {
        //内存
        setToken("");
        //数据库
        SQLiteOpenManager.getInstance().doDeleteDataAction(DataBaseTable.Tables.TABLE_USER, null, null);
    }


    public String queryToken() {
        String sql = "select * from " + DataBaseTable.Tables.TABLE_USER;
        String token = "";
        Cursor cursor = SQLiteOpenManager.getInstance().doQueryDataAction(sql);
        if (cursor != null && cursor.moveToFirst()) {

            int columnIndex = cursor.getColumnIndex(DataBaseTable.UserTableColumns._TOKEN);
            if (columnIndex != -1) {
                token = cursor.getString(columnIndex);
            }
            cursor.close();
        }
        if (!TextUtils.isEmpty(token)) {
            setToken(token);
        }
        return token;
    }

    public UserInfo queryUserInfo() {
        String sql = "select * from " + DataBaseTable.Tables.TABLE_USER;
        UserInfo userInfo = null;
        Cursor cursor = SQLiteOpenManager.getInstance().doQueryDataAction(sql);
        if (cursor != null && cursor.moveToFirst()) {
            userInfo = new UserInfo();
            String token = cursor.getString(cursor.getColumnIndex(DataBaseTable.UserTableColumns._TOKEN));
            userInfo.setToken(token);

            String headImage = cursor.getString(cursor.getColumnIndex(DataBaseTable.UserTableColumns._HEAD_IMAGE));
            userInfo.setHeadImage(headImage);

            String userName = cursor.getString(cursor.getColumnIndex(DataBaseTable.UserTableColumns._USER_NAME));
            userInfo.setUserName(userName);

            cursor.close();
        }
        return userInfo;
    }


    public UserInfo getUserInfo() throws Exception {
        UserInfo userInfo = mApiMy.getUserInfo();
        setUserInfo(userInfo);
        return userInfo;
    }

    /*用户登录*/
    public UserInfo userLogin(String platform, String userName, String password, String clientId, String imei) throws Exception {
        UserInfo userInfo = mApiMy.UserLogin(platform, userName, password, clientId, imei);
        cacheUserInfo(userInfo);
        setUserInfo(userInfo);
        return userInfo;
    }

    /*用户注册*/
    public UserInfo userRegister(String platform, String userName, String password, String clientId, String code, String imei) throws Exception {
        UserInfo userInfo = mApiMy.UserRegister(platform, userName, password, clientId, code, imei);
        cacheUserInfo(userInfo);
        setUserInfo(userInfo);
        return userInfo;
    }

    /*获取注册验证码*/
    public CodeInfo getRegisterCode(String phone) throws Exception {
        return mApiMy.GetRegisterCode(phone);
    }

    /*微信登录*/
    public UserInfo wechatLogin(String platform, String wechatCode, String clientId, String imei) throws Exception {
        UserInfo userInfo = mApiMy.WechatLogin(platform, wechatCode, clientId, imei);
        cacheUserInfo(userInfo);
        setUserInfo(userInfo);
        return userInfo;
    }

    /*微信绑定*/
    public UserInfo bindWechat(String wechatCode) throws Exception {
        UserInfo userInfo = mApiMy.BindWechat(wechatCode);
        return userInfo;
    }

    /*微信认证*/
    public UserInfo wechatAuth(String platform, String wechatCode, String clientId, String userName, String code, String imei) throws Exception {
        UserInfo userInfo = mApiMy.WechatAuth(platform, wechatCode, clientId, userName, code, imei);
        cacheUserInfo(userInfo);
        setUserInfo(userInfo);
        return userInfo;
    }

    /*微信注册*/
    public UserInfo wechatRegister(String platform, String wechatCode, String clientId, String userName, String password, String imei) throws Exception {
        UserInfo userInfo = mApiMy.WechatRegister(platform, wechatCode, clientId, userName, password, imei);
        cacheUserInfo(userInfo);
        setUserInfo(userInfo);
        return userInfo;
    }

    /*获取忘记密码验证码*/
    public CodeInfo getForgotPwdCode(String phone) throws Exception {
        return mApiMy.GetForgotPwdCode(phone);
    }

    /*获取微信绑定验证码*/
    public CodeInfo getWechatBindCode(String phone) throws Exception {
        return mApiMy.GetWechatBindCode(phone);
    }

    /*检验忘记密码验证码*/
    public CodeInfo checkPwdCode(String verifyCode, String phone) throws Exception {
        return mApiMy.CheckForgotPwdCode(verifyCode, phone);
    }

    /*重设密码*/
    public UserInfo resetLoginPwd(String newPassword, String phone, String code, String clientId, String imei, String platform) throws Exception {
        UserInfo userInfo = mApiMy.ResetLoginPwd(newPassword, phone, code, clientId, imei, platform);
        cacheUserInfo(userInfo);
        setUserInfo(userInfo);
        return userInfo;
    }


    private void cacheUserInfo(UserInfo userInfo) {
        if (userInfo != null) {
            mToken = userInfo.getToken();
            SQLiteOpenManager.getInstance().doDeleteDataAction(DataBaseTable.Tables.TABLE_USER, null, null);
            ContentValues values = new ContentValues();
            values.put(DataBaseTable.UserTableColumns._HEAD_IMAGE, userInfo.getHeadImage());
            values.put(DataBaseTable.UserTableColumns._USER_NAME, userInfo.getUserName());
            values.put(DataBaseTable.UserTableColumns._TOKEN, userInfo.getToken());
            SQLiteOpenManager.getInstance().doSaveDataAction(DataBaseTable.Tables.TABLE_USER, values, null, null);
        }
    }


    public ErrorMessage getSetting() throws Exception {
        return mApiMy.getSetting();
    }

    @Override
    public ArrayList<Class<?>> getTableClazzDeclare() {
        ArrayList<Class<?>> arrayClazz = new ArrayList<>();
        arrayClazz.add(DataBaseTable.UserTableColumns.class);
        return arrayClazz;
    }

}
